package cn.e4j.modular.api.service;


import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.api.mapper.BfcglMapper;
import cn.e4j.modular.system.entity.SysOrg;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Comparator;
import java.util.List;

@Service
public class BfcglService {


    @Autowired
    BfcglMapper bfcglMapper;


    @Transactional(readOnly = true)
    public List<Dto> getInfo(Dto indto){
        List<Dto> total = InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.BfcglMapper.getCount",indto);
        indto.put("isFinish",1);
        List<Dto> finish = InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.BfcglMapper.getCount",indto);
        BigDecimal decimal = new BigDecimal(100);
        for (Dto d:total){
            String gwbh002 = d.getString("GWBH002");
            d.put("percent",0);
            d.put("finish",0);
            for (Dto f:finish){
                if (f.getString("GWBH002").equals(gwbh002)){
                    //计算百分比
                    BigDecimal finished = new BigDecimal(f.getInteger("COUNT"));
                    BigDecimal total_ = new BigDecimal(d.getInteger("COUNT"));
                    BigDecimal pecent = finished.divide(total_,3,RoundingMode.HALF_UP).multiply(decimal);
                    d.put("percent",pecent.doubleValue());
                    d.put("finish",f.getInteger("COUNT"));
                }
            }
        }

        total.sort(Comparator.comparing(dto->dto.getString("percent")));

        return total;
    }
}
